using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class UIController : MonoBehaviour
{
    public UIDocument document;
    public VisualElement root;
    private Button _button;

    private Label _label;
    // Start is called before the first frame update
    void Start()
    {
        document = GetComponent<UIDocument>();
        if (document == null)
        {
            Debug.LogError("UI获取文档失败！");
            return;
        }

        root = document.rootVisualElement;
        init();
    }

    public void init()
    {
        _button = root.Q<Button>(classes: "collect-btn");
        _label = root.Q<Label>(classes: "info");
        _button.clicked += () =>
        {
            EventManager.getInstance().dispatcher(new NetConnectEvent());
        };
        EventManager.getInstance().register(EventIdDefine.NetConnectSuccess, (e) =>
        {
            _button.SetEnabled(false);
            _button.text = "连接已成功！";
            _label.text = "成功连接服务器！";
            Req_Login login = new Req_Login();
            NetWebSocket.SendMsg(login);
        });
        EventManager.getInstance().register(EventIdDefine.NetError, (e) =>
        {
            _label.text = "连接出错："+(e as NetErrorEvent).msg;
        });
        EventManager.getInstance().register(EventIdDefine.NetConnectClosed, (e) =>
        {
            _label.text = "连接已关闭！";
            _button.SetEnabled(true);
            _button.text = "连接服务器";
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
